package com.zmm.common.base.model;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import org.apache.http.HttpStatus;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 900045
 * @description:
 * @name Result
 * @date By 2021-06-22 10:45:00
 */
public class Result extends HashMap<String,Object> implements Serializable {
	
	private static final long serialVersionUID = 5806450120374554121L;


	/**
	 * 将返回数据封装到 map中 key:data value:查询数据
	 * @param data
	 * @return
	 */
	public Result setData(Object data){
		put("data",data);
		return this;
	}

	/**
	 * 默认 key 为 data 使用 fastJSON 进行逆转
	 * @param typeReference
	 * @param <T>
	 * @return
	 */
	public <T>T getData(TypeReference<T> typeReference){
		// get() --->Map类型
		Object data = get("data");
		String jsonString = JSON.toJSONString(data);
		T t = JSON.parseObject(jsonString, typeReference);
		return t;
	}

	/**
	 * 自定义 key 使用 fastJSON 进行逆转
	 * @param key
	 * @param typeReference
	 * @param <T>
	 * @return
	 */
	public <T>T getData(String key,TypeReference<T> typeReference){
		// get() --->Map类型
		Object data = get(key);
		String jsonString = JSON.toJSONString(data);
		T t = JSON.parseObject(jsonString, typeReference);
		return t;
	}

	public Result() {
		put("code", 0);
		put("msg", "success");
	}

	public static Result ok(String msg) {
		Result r = new Result();
		r.put("msg", msg);
		return r;
	}

	public static Result ok(ResultCode resultCode){
		Result result = new Result();
		result.put("code", resultCode.getCode());
		result.put("msg", resultCode.getDesc());
		return result;
	}
	
	public static Result ok(Map<String, Object> map) {
		Result r = new Result();
		r.putAll(map);
		return r;
	}

	public static Result ok() {
		return new Result();
	}

	public Integer getCode() {
		return (Integer) this.get("code");
	}

	public static Result error(int code, String msg) {
		Result r = new Result();
		r.put("code", code);
		r.put("msg", msg);
		return r;
	}
	
	public static Result error(ResponseCode code,String msg){
		Result r = new Result();
		r.put("code", code.getCode());
		r.put("msg", msg);
		return r;
	}
	
	public static Result error(ResultCode resultCode,Map<String ,String> msg){
		Result result = new Result();
		result.put("code", resultCode.getCode());
		result.put("msg", msg);
		return result;
	}
	
	/**
	 * 返回结果响应 参数为 ResultCode
	 * @author: 900045
	 * @date: 2021-07-06 09:40:39
	 * @throws 
	 * @param resultCode: 
	 * @return: com.zmm.common.base.model.Result
	 **/
	public static Result error(ResultCode resultCode){
		Result result = new Result();
		result.put("code", resultCode.getCode());
		result.put("msg", resultCode.getDesc());
		return result;
	}
	
	public static Result error() {
		return error(HttpStatus.SC_INTERNAL_SERVER_ERROR, "未知异常，请联系管理员");
	}

	public static Result error(String msg) {
		return error(HttpStatus.SC_INTERNAL_SERVER_ERROR,msg);
	}

	@Override
	public Object put(String key, Object value) {
		super.put(key, value);
		return this;
	}
}
